#version 140

precision highp float;

uniform sampler2D Tex;
uniform sampler2D Depth;

uniform float Near;
uniform float Far;

in vec2 TexCoord;

out vec4 FragColor;

float LinearDepth(float d, float n, float f)
{
	return (2.0 * n) / (f + n - d * (f - n));	
}

void main()
{	
	vec3 tex = texture2D(Tex, TexCoord).rgb;
	vec3 fogColor = vec3(0.3, 0.4, 0.7);
	
	// calculate fog
	float d = 1.0 - LinearDepth(texture2D(Depth, TexCoord).r, Near, Far);
	float z = d * Far * 0.01;
	float fogFactor = exp2(-z * z * 1.442695);

	FragColor = vec4(mix(tex,fogColor,0), 1.0);
}